<template>
  <div class="meetings-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>会议列表</span>
          <el-button type="primary" @click="handleAdd">新增会议</el-button>
        </div>
      </template>

      <el-table :data="meetingList" v-loading="loading" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="title" label="会议主题" />
        <el-table-column prop="host" label="主持人" width="120" />
        <el-table-column prop="time" label="会议时间" width="180" />
        <el-table-column prop="location" label="会议地点" width="150" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getMeetingStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <div class="operation-buttons">
              <el-button size="small" @click="handleView(scope.row)" class="operation-button view-button">查看</el-button>
              <el-button size="small" @click="handleEdit(scope.row)" class="operation-button edit-button">编辑</el-button>
              <el-button size="small" type="warning" @click="handleAnalyze(scope.row)"
                class="operation-button analyze-button">分析</el-button>
              <el-button size="small" type="danger" @click="handleDelete(scope.row)"
                class="operation-button delete-button">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination v-model:current-page="pagination.page" v-model:page-size="pagination.size"
          :page-sizes="[10, 20, 50]" :total="pagination.total" layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </el-card>

    <!-- 分析过程弹框 -->
    <el-dialog v-model="analyzeDialogVisible" title="会议分析过程" width="50%" :before-close="handleAnalyzeClose"
      class="analysis-dialog">
      <div class="analysis-content">
        <el-steps :active="analysisStep" finish-status="success" simple>
          <el-step title="音频文件上传" />
          <el-step title="音频转文本" />
          <el-step title="文本分析" />
          <el-step title="生成报告" />
        </el-steps>

        <div class="analysis-details" v-if="currentMeeting">
          <h4>会议信息</h4>
          <p>会议主题: {{ currentMeeting.title }}</p>
          <p>会议时间: {{ currentMeeting.time }}</p>

          <div class="progress-container" v-if="isAnalyzing">
            <el-progress :percentage="analysisProgress" :status="analysisProgress === 100 ? 'success' : ''"
              style="margin: 20px 0" />
            <p>{{ analysisStatus }}</p>
          </div>
        </div>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="analyzeDialogVisible = false" class="footer-button cancel-button">取消</el-button>
          <el-button type="primary" @click="startAnalysis" :loading="isAnalyzing"
            v-if="!isAnalyzing || analysisProgress < 100" class="footer-button confirm-button">
            {{ isAnalyzing ? '分析中...' : '开始分析' }}
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'

export default {
  name: 'Meetings',
  setup() {
    const router = useRouter()
    const loading = ref(false)
    const meetingList = ref([])

    // 分析相关状态
    const analyzeDialogVisible = ref(false)
    const isAnalyzing = ref(false)
    const analysisStep = ref(0)
    const analysisProgress = ref(0)
    const analysisStatus = ref('等待开始...')
    const currentMeeting = ref(null)

    const pagination = reactive({
      page: 1,
      size: 10,
      total: 0
    })

    // 模拟会议数据
    const mockMeetingList = [
      {
        id: 1,
        title: '季度工作总结会议',
        host: '张经理',
        time: '2023-04-15 14:00:00',
        location: '会议室A',
        status: '已完成',
        audioFiles: ['audio1.mp3', 'audio2.mp3']
      },
      {
        id: 2,
        title: '产品需求评审会',
        host: '李总监',
        time: '2023-04-16 10:00:00',
        location: '会议室B',
        status: '进行中',
        audioFiles: ['audio3.mp3']
      },
      {
        id: 3,
        title: '技术方案讨论会',
        host: '王工程师',
        time: '2023-04-17 15:30:00',
        location: '会议室C',
        status: '未开始',
        audioFiles: []
      }
    ]

    // 获取会议列表
    const fetchMeetingList = async () => {
      try {
        loading.value = true
        // 模拟API调用延迟
        await new Promise(resolve => setTimeout(resolve, 500))
        meetingList.value = mockMeetingList
        pagination.total = mockMeetingList.length
      } catch (error) {
        ElMessage.error('获取会议列表失败: ' + error.message)
      } finally {
        loading.value = false
      }
    }

    // 获取会议状态标签类型
    const getMeetingStatusType = (status) => {
      switch (status) {
        case '已完成':
          return 'success'
        case '进行中':
          return 'warning'
        case '未开始':
          return 'info'
        default:
          return 'info'
      }
    }

    // 处理分页大小变化
    const handleSizeChange = (val) => {
      pagination.size = val
      fetchMeetingList()
    }

    // 处理当前页变化
    const handleCurrentChange = (val) => {
      pagination.page = val
      fetchMeetingList()
    }

    // 处理新增会议
    const handleAdd = () => {
      router.push('/meeting/add')
    }

    // 处理查看会议
    const handleView = (row) => {
      router.push(`/meeting/detail?id=${row.id}`)
    }

    // 处理编辑会议
    const handleEdit = (row) => {
      router.push(`/meeting/edit?id=${row.id}`)
    }

    // 处理删除会议
    const handleDelete = (row) => {
      ElMessageBox.confirm(`确定要删除会议 "${row.title}" 吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 模拟删除操作
        ElMessage.success('删除成功')
        fetchMeetingList()
      }).catch(() => {
        // 取消删除
      })
    }

    // 处理开始分析
    const handleAnalyze = (row) => {
      currentMeeting.value = row
      analyzeDialogVisible.value = true
      resetAnalysis()
    }

    // 重置分析状态
    const resetAnalysis = () => {
      isAnalyzing.value = false
      analysisStep.value = 0
      analysisProgress.value = 0
      analysisStatus.value = '等待开始...'
    }

    // 开始分析
    const startAnalysis = () => {
      if (isAnalyzing.value) return

      isAnalyzing.value = true
      analysisProgress.value = 0
      analysisStatus.value = '正在上传音频文件...'

      // 模拟分析过程
      const interval = setInterval(() => {
        analysisProgress.value += 5

        if (analysisProgress.value >= 25 && analysisStep.value === 0) {
          analysisStep.value = 1
          analysisStatus.value = '正在进行音频转文本...'
        } else if (analysisProgress.value >= 50 && analysisStep.value === 1) {
          analysisStep.value = 2
          analysisStatus.value = '正在进行文本分析...'
        } else if (analysisProgress.value >= 75 && analysisStep.value === 2) {
          analysisStep.value = 3
          analysisStatus.value = '正在生成分析报告...'
        } else if (analysisProgress.value >= 100) {
          analysisStatus.value = '分析完成！'
          clearInterval(interval)
          setTimeout(() => {
            ElMessage.success('会议分析完成！')
          }, 500)
        }
      }, 200)
    }

    // 处理分析弹框关闭
    const handleAnalyzeClose = (done) => {
      if (isAnalyzing.value && analysisProgress.value < 100) {
        ElMessageBox.confirm('分析正在进行中，确定要关闭吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          done()
        }).catch(() => {
          // 取消关闭
        })
      } else {
        done()
      }
    }

    // 组件挂载时获取数据
    onMounted(() => {
      fetchMeetingList()
    })

    return {
      loading,
      meetingList,
      pagination,
      analyzeDialogVisible,
      isAnalyzing,
      analysisStep,
      analysisProgress,
      analysisStatus,
      currentMeeting,
      fetchMeetingList,
      getMeetingStatusType,
      handleSizeChange,
      handleCurrentChange,
      handleAdd,
      handleView,
      handleEdit,
      handleDelete,
      handleAnalyze,
      startAnalysis,
      handleAnalyzeClose
    }
  }
}
</script>

<style scoped>
.meetings-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

/* 优化操作列按钮样式 */
.operation-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-start;
}

.operation-button {
  margin: 2px 0;
}

.view-button {
  background-color: #ecf5ff;
  border-color: #b3d8ff;
  color: #409eff;
}

.view-button:hover {
  background-color: #409eff;
  border-color: #409eff;
  color: #fff;
}

.edit-button {
  background-color: #f0f9eb;
  border-color: #c2e7b0;
  color: #67c23a;
}

.edit-button:hover {
  background-color: #67c23a;
  border-color: #67c23a;
  color: #fff;
}

.analyze-button {
  background-color: #fdf6ec;
  border-color: #f5dab1;
  color: #e6a23c;
}

.analyze-button:hover {
  background-color: #e6a23c;
  border-color: #e6a23c;
  color: #fff;
}

.delete-button {
  background-color: #fef0f0;
  border-color: #fbc4c4;
  color: #f56c6c;
}

.delete-button:hover {
  background-color: #f56c6c;
  border-color: #f56c6c;
  color: #fff;
}

.analysis-content {
  padding: 20px 0;
}

.analysis-details h4 {
  margin-top: 0;
}

.analysis-details p {
  margin: 5px 0;
}

.progress-container {
  text-align: center;
}

/* 优化弹框样式 */
.analysis-dialog {
  border-radius: 8px;
  overflow: hidden;
}

.analysis-dialog :deep(.el-dialog__header) {
  background-color: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
  padding: 15px 20px;
}

.analysis-dialog :deep(.el-dialog__title) {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.analysis-dialog :deep(.el-dialog__body) {
  padding: 20px;
}

.analysis-dialog :deep(.el-dialog__footer) {
  padding: 15px 20px;
  background-color: #f5f7fa;
  border-top: 1px solid #ebeef5;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
}

.footer-button {
  border-radius: 4px;
  padding: 8px 20px;
}

.cancel-button {
  border-color: #dcdfe6;
  color: #606266;
}

.cancel-button:hover {
  background-color: #f5f7fa;
  border-color: #c0c4cc;
  color: #606266;
}

.confirm-button {
  background-color: #409eff;
  border-color: #409eff;
}

.confirm-button:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

/* 暗色主题适配 */
html.dark .analysis-dialog :deep(.el-dialog__header) {
  background-color: #1d1e1f;
  border-bottom-color: #4c4d4f;
}

html.dark .analysis-dialog :deep(.el-dialog__title) {
  color: #e5eaf3;
}

html.dark .analysis-dialog :deep(.el-dialog__footer) {
  background-color: #1d1e1f;
  border-top-color: #4c4d4f;
}

html.dark .cancel-button {
  background-color: #1d1e1f;
  border-color: #4c4d4f;
  color: #cfd3dc;
}

html.dark .cancel-button:hover {
  background-color: #2d2e2f;
  border-color: #606266;
  color: #e5eaf3;
}
</style>